home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / winsr173.zip / LORENZ.C < prev    next >
C/C++ Source or Header  |  1992-01-12  |  46KB  |  1,718 lines

  1. /*
  2.    This file contains two 3 dimensional orbit-type fractal
  3.    generators - IFS and LORENZ3D, along with code to generate
  4.    red/blue 3D images. Tim Wegner
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <float.h>
  10. #include <math.h>
  11. #include "fractint.h"
  12. #include "fractype.h"
  13.  
  14. struct affine
  15. {
  16.    /* weird order so a,b,e and c,d,f are vectors */
  17.    double a;
  18.    double b;
  19.    double e;
  20.    double c;
  21.    double d;
  22.    double f;
  23. };
  24. struct l_affine
  25. {
  26.    /* weird order so a,b,e and c,d,f are vectors */
  27.    long a;
  28.    long b;
  29.    long e;
  30.    long c;
  31.    long d;
  32.    long f;
  33. };
  34. struct long3dvtinf /* data used by 3d view transform subroutine */
  35. {
  36.    long ct;        /* iteration counter */
  37.    long orbit[3];    /* interated function orbit value */
  38.    long iview[3];    /* perspective viewer's coordinates */
  39.    long viewvect[3];    /* orbit transformed for viewing */
  40.    long viewvect1[3];    /* orbit transformed for viewing */
  41.    long maxvals[3];
  42.    long minvals[3];
  43.    MATRIX doublemat;    /* transformation matrix */
  44.    MATRIX doublemat1;    /* transformation matrix */
  45.    long longmat[4][4];    /* long version of matrix */
  46.    long longmat1[4][4]; /* long version of matrix */
  47.    int row,col;     /* results */
  48.    int row1,col1;
  49.    struct l_affine cvt;
  50. };
  51. struct float3dvtinf /* data used by 3d view transform subroutine */
  52. {
  53.    long ct;        /* iteration counter */
  54.    double orbit[3];           /* interated function orbit value */
  55.    double viewvect[3];          /* orbit transformed for viewing */
  56.    double viewvect1[3];        /* orbit transformed for viewing */
  57.    double maxvals[3];
  58.    double minvals[3];
  59.    MATRIX doublemat;    /* transformation matrix */
  60.    MATRIX doublemat1;    /* transformation matrix */
  61.    int row,col;     /* results */
  62.    int row1,col1;
  63.    struct affine cvt;
  64. };
  65.  
  66. /* Routines in this module    */
  67.  
  68. int  orbit3dlongsetup();
  69. int  orbit3dfloatsetup();
  70. int  lorenz3dlongorbit(long *, long *, long *);
  71. int  lorenz3dfloatorbit(double *, double *, double *);
  72. int  lorenz3d1floatorbit(double *, double *, double *);
  73. int  lorenz3d3floatorbit(double *, double *, double *);
  74. int  lorenz3d4floatorbit(double *, double *, double *);
  75. int  henonfloatorbit(double *, double *, double *);
  76. int  henonlongorbit(long *, long *, long *);
  77. int  rosslerfloatorbit(double *, double *, double *);
  78. int  pickoverfloatorbit(double *, double *, double *);
  79. int  gingerbreadfloatorbit(double *, double *, double *);
  80. int  rosslerlongorbit(long *, long *, long *);
  81. int  kamtorusfloatorbit(double *, double *, double *);
  82. int  kamtoruslongorbit(long *, long *, long *);
  83. int  orbit2dfloat(void);
  84. int  orbit2dlong(void);
  85. int  orbit3dlongcalc(void);
  86. int  orbit3dfloatcalc(void);
  87. int  funny_glasses_call(int (*calc)());
  88. int  ifs(void);
  89. int  orbit3dfloat(void);
  90. int  orbit3dlong(void);
  91. int  ifs2d(void);
  92. int  ifs3d(void);
  93.  
  94. static int  ifs3dlong(void);
  95. static int  ifs3dfloat(void);
  96. static double determinant(double mat[3][3]);
  97. static int  solve3x3(double mat[3][3],double vec[3],double ans[3]);
  98. static int  setup_convert_to_screen(struct affine *);
  99. static int  l_setup_convert_to_screen(struct l_affine *);
  100. static void setupmatrix(MATRIX);
  101. static int  long3dviewtransf(struct long3dvtinf *inf);
  102. static int  float3dviewtransf(struct float3dvtinf *inf);
  103. static FILE *open_orbitsave();
  104.  
  105. static int realtime;
  106. extern int orbitsave;
  107. static char orbitsavename[]    = {"orbits.raw"};
  108. static char orbitsave_format[] = {"%g %g %g 15\n"};
  109. extern int active_system;
  110. extern int overflow;
  111. extern int soundflag;
  112. extern int basehertz;
  113. extern int fractype;
  114. extern int glassestype;
  115. extern int whichimage;
  116. extern int init3d[];
  117. extern char floatflag;
  118. extern VECTOR view;
  119. extern int xxadjust, yyadjust;
  120. extern int xxadjust1, yyadjust1;
  121. extern int xshift,yshift;
  122. extern int xshift1,yshift1;
  123. extern int    debugflag;            /* for debugging purposes */
  124. extern int    xdots, ydots;        /* coordinates of dots on the screen  */
  125. extern int    maxit;                /* try this many iterations */
  126. extern double param[];
  127. extern double    xxmin,xxmax,yymin,yymax,xx3rd,yy3rd; /* selected screen corners  */
  128. extern    int    diskvideo;            /* for disk-video klooges */
  129. extern int    bitshift;            /* bit shift for fudge */
  130. extern long    fudge;                /* fudge factor (2**n) */
  131. extern int    colors;             /* maximum colors available */
  132. extern int display3d;
  133.  
  134. extern float far *ifs_defn;
  135. extern int ifs_type;
  136.  
  137. static int t;
  138. static long l_dx,l_dy,l_dz,l_dt,l_a,l_b,l_c,l_d;
  139. static long l_adt,l_bdt,l_cdt,l_xdt,l_ydt;
  140. static long l_initx,l_inity,l_initz;
  141. static long initorbitlong[3];
  142.  
  143. static double dx,dy,dz,dt,a,b,c,d;
  144. static double adt,bdt,cdt,xdt,ydt,zdt;
  145. static double initx,inity,initz;
  146. static double initorbit[3];
  147. extern int inside;
  148.  
  149. extern int  alloc_resume(int,int);
  150. extern int  start_resume();
  151. extern void end_resume();
  152. extern int  put_resume(int len, ...);
  153. extern int  get_resume(int len, ...);
  154. extern int  calc_status, resuming;
  155. extern int  diskisactive;
  156. extern char savename[];
  157.  
  158. /* these are potential user parameters */
  159. int connect = 1;    /* flag to connect points with a line */
  160. int waste = 100;    /* waste this many points before plotting */
  161. int projection = 2; /* projection plane - default is to plot x-y */
  162.  
  163. /******************************************************************/
  164. /*           zoom box conversion functions          */
  165. /******************************************************************/
  166.  
  167. static double determinant(mat) /* determinant of 3x3 matrix */
  168. double mat[3][3];
  169. {
  170.    /* calculate determinant of 3x3 matrix */
  171.    return(mat[0][0]*mat[1][1]*mat[2][2] +
  172.       mat[0][2]*mat[1][0]*mat[2][1] +
  173.       mat[0][1]*mat[1][2]*mat[2][0] -
  174.       mat[2][0]*mat[1][1]*mat[0][2] -
  175.       mat[1][0]*mat[0][1]*mat[2][2] -
  176.       mat[0][0]*mat[1][2]*mat[2][1]);
  177.  
  178. }
  179.  
  180. static int solve3x3(mat,vec,ans) /* solve 3x3 inhomogeneous linear equations */
  181. double mat[3][3], vec[3], ans[3];
  182. {
  183.    /* solve 3x3 linear equation [mat][ans] = [vec] */
  184.    double denom;
  185.    double tmp[3][3];
  186.    int i;
  187.    denom = determinant(mat);
  188.    if(fabs(denom) < DBL_EPSILON) /* test if can solve */
  189.      return(-1);
  190.    memcpy(tmp,mat,sizeof(double)*9);
  191.    for(i=0;i<3;i++)
  192.    {
  193.       tmp[0][i] = vec[0];
  194.       tmp[1][i] = vec[1];
  195.       tmp[2][i] = vec[2];
  196.       ans[i]  =  determinant(tmp)/denom;
  197.       tmp[0][i] = mat[0][i];
  198.       tmp[1][i] = mat[1][i];
  199.       tmp[2][i] = mat[2][i];
  200.     }
  201.     return(0);
  202. }
  203.  
  204.  
  205. /* Conversion of complex plane to screen coordinates for rotating zoom box.
  206.    Assume there is an affine transformation mapping complex zoom parallelogram
  207.    to rectangular screen. We know this map must map parallelogram corners to
  208.    screen corners, so we have following equations:
  209.  
  210.       a*xxmin+b*yymax+e == 0        (upper left)
  211.       c*xxmin+d*yymax+f == 0
  212.  
  213.       a*xx3rd+b*yy3rd+e == 0        (lower left)
  214.       c*xx3rd+d*yy3rd+f == ydots-1
  215.  
  216.       a*xxmax+b*yymin+e == xdots-1  (lower right)
  217.       c*xxmax+d*yymin+f == ydots-1
  218.  
  219.       First we must solve for a,b,c,d,e,f - (which we do once per image),
  220.       then we just apply the transformation to each orbit value.
  221. */
  222.  
  223. static int setup_convert_to_screen(struct affine *scrn_cnvt)
  224. {
  225.    /* we do this twice - rather than having six equations with six unknowns,
  226.       everything partitions to two sets of three equations with three
  227.       unknowns. Nice, because who wants to calculate a 6x6 determinant??
  228.     */
  229.    double mat[3][3];
  230.    double vec[3];
  231.    /*
  232.       first these equations - solve for a,b,e
  233.       a*xxmin+b*yymax+e == 0        (upper left)
  234.       a*xx3rd+b*yy3rd+e == 0        (lower left)
  235.       a*xxmax+b*yymin+e == xdots-1  (lower right)
  236.    */
  237.    mat[0][0] = xxmin;
  238.    mat[0][1] = yymax;
  239.    mat[0][2] = 1.0;
  240.    mat[1][0] = xx3rd;
  241.    mat[1][1] = yy3rd;
  242.    mat[1][2] = 1.0;
  243.    mat[2][0] = xxmax;
  244.    mat[2][1] = yymin;
  245.    mat[2][2] = 1.0;
  246.    vec[0]    = 0.0;
  247.    vec[1]    = 0.0;
  248.    vec[2]    = (double)(xdots-1);
  249.  
  250.    if(solve3x3(mat,vec, &(scrn_cnvt->a)))
  251.       return(-1);
  252.    /*
  253.       now solve these:
  254.       c*xxmin+d*yymax+f == 0
  255.       c*xx3rd+d*yy3rd+f == ydots-1
  256.       c*xxmax+d*yymin+f == ydots-1
  257.       (mat[][] has not changed - only vec[])
  258.    */
  259.    vec[0]    = 0.0;
  260.    vec[1]    = (double)(ydots-1);
  261.    vec[2]    = (double)(ydots-1);
  262.  
  263.    if(solve3x3(mat,vec, &scrn_cnvt->c))
  264.       return(-1);
  265.    return(0);
  266. }
  267.  
  268. static int l_setup_convert_to_screen(struct l_affine *l_cvt)
  269. {
  270.    struct affine cvt;
  271.  
  272.    /* MCP 7-7-91, This function should return a something! */
  273.    if(setup_convert_to_screen(&cvt))
  274.       return(-1);
  275.    l_cvt->a = cvt.a*fudge; l_cvt->b = cvt.b*fudge; l_cvt->c = cvt.c*fudge;
  276.    l_cvt->d = cvt.d*fudge; l_cvt->e = cvt.e*fudge; l_cvt->f = cvt.f*fudge;
  277.  
  278.    /* MCP 7-7-91 */
  279.    return(0);
  280. }
  281.  
  282. /******************************************************************/
  283. /*   setup functions - put in fractalspecific[fractype].per_image */
  284. /******************************************************************/
  285.  
  286. static double orbit;
  287. static long   l_orbit;
  288.  
  289. extern double sinx,cosx;
  290. static long l_sinx,l_cosx;
  291.  
  292. int orbit3dlongsetup()
  293. {
  294.    connect = 1;
  295.    waste = 100;
  296.    projection = 2;
  297.    if(fractype==LHENON || fractype==KAM || fractype==KAM3D)
  298.       connect=0;
  299.    if(fractype==LROSSLER)
  300.       waste = 500;
  301.    if(fractype==LLORENZ)
  302.       projection = 1;
  303.  
  304.    initorbitlong[0] = fudge;  /* initial conditions */
  305.    initorbitlong[1] = fudge;
  306.    initorbitlong[2] = fudge;
  307.  
  308.    if(fractype==LHENON)
  309.    {
  310.       l_a =  param[0]*fudge;
  311.       l_b =  param[1]*fudge;
  312.       l_c =  param[2]*fudge;
  313.       l_d =  param[3]*fudge;
  314.    }
  315.    else if(fractype==KAM || fractype==KAM3D)
  316.    {
  317.       a   = param[0];        /* angle */
  318.       if(param[1] <= 0.0)
  319.      param[1] = .01;
  320.       l_b =  param[1]*fudge;    /* stepsize */
  321.       l_c =  param[2]*fudge;    /* stop */
  322.       t = l_d =  param[3];     /* points per orbit */
  323.  
  324.       l_sinx = sin(a)*fudge;
  325.       l_cosx = cos(a)*fudge;
  326.       l_orbit = 0;
  327.       initorbitlong[0] = initorbitlong[1] = initorbitlong[2] = 0;
  328.    }
  329.    else
  330.    {
  331.       l_dt = param[0]*fudge;
  332.       l_a =  param[1]*fudge;
  333.       l_b =  param[2]*fudge;
  334.       l_c =  param[3]*fudge;
  335.    }
  336.  
  337.    /* precalculations for speed */
  338.    l_adt = multiply(l_a,l_dt,bitshift);
  339.    l_bdt = multiply(l_b,l_dt,bitshift);
  340.    l_cdt = multiply(l_c,l_dt,bitshift);
  341.    return(1);
  342. }
  343.  
  344. int orbit3dfloatsetup()
  345. {
  346.    connect = 1;
  347.    waste = 100;
  348.    projection = 2;
  349.  
  350.    if(fractype==FPHENON || fractype==FPPICKOVER || fractype==FPGINGERBREAD
  351.         || fractype == KAMFP || fractype == KAM3DFP
  352.         || fractype == FPHOPALONG)
  353.       connect=0;
  354.    if(fractype==FPLORENZ3D1 || fractype==FPLORENZ3D3 ||
  355.       fractype==FPLORENZ3D4)
  356.       waste = 750;
  357.    if(fractype==FPROSSLER)
  358.       waste = 500;
  359.    if(fractype==FPLORENZ)
  360.       projection = 1; /* plot x and z */
  361.  
  362.    initorbit[0] = 1;  /* initial conditions */
  363.    initorbit[1] = 1;
  364.    initorbit[2] = 1;
  365.    if(fractype==FPGINGERBREAD)
  366.    {
  367.       initorbit[0] = param[0];    /* initial conditions */
  368.       initorbit[1] = param[1];
  369.    }
  370.    if(fractype==FPHENON || fractype==FPPICKOVER)
  371.    {
  372.       a =  param[0];
  373.       b =  param[1];
  374.       c =  param[2];
  375.       d =  param[3];
  376.    }
  377.    else if(fractype==KAMFP || fractype==KAM3DFP)
  378.    {
  379.       a = param[0];          /* angle */
  380.       if(param[1] <= 0.0)
  381.      param[1] = .01;
  382.       b =  param[1];    /* stepsize */
  383.       c =  param[2];    /* stop */
  384.       t = l_d =  param[3];     /* points per orbit */
  385.       sinx = sin(a);
  386.       cosx = cos(a);
  387.       orbit = 0;
  388.       initorbit[0] = initorbit[1] = initorbit[2] = 0;
  389.    } else if(fractype==FPHOPALONG || fractype==FPMARTIN)
  390.  
  391.    {
  392.       initorbit[0] = 0;  /* initial conditions */
  393.       initorbit[1] = 0;
  394.       initorbit[2] = 0;
  395.       connect = 0;
  396.       a =  param[0];
  397.       b =  param[1];
  398.       c =  param[2];
  399.       d =  param[3];
  400.    } else
  401.    {
  402.       dt = param[0];
  403.       a =  param[1];
  404.       b =  param[2];
  405.       c =  param[3];
  406.    }
  407.  
  408.    /* precalculations for speed */
  409.    adt = a*dt;
  410.    bdt = b*dt;
  411.    cdt = c*dt;
  412.  
  413.    return(1);
  414. }
  415.  
  416. /******************************************************************/
  417. /*   orbit functions - put in fractalspecific[fractype].orbitcalc */
  418. /******************************************************************/
  419.  
  420. int lorenz3dlongorbit(long *l_x, long *l_y, long *l_z)
  421. {
  422.       l_xdt = multiply(*l_x,l_dt,bitshift);
  423.       l_ydt = multiply(*l_y,l_dt,bitshift);
  424.       l_dx  = -multiply(l_adt,*l_x,bitshift) + multiply(l_adt,*l_y,bitshift);
  425.       l_dy  =  multiply(l_bdt,*l_x,bitshift) -l_ydt -multiply(*l_z,l_xdt,bitshift);
  426.       l_dz  = -multiply(l_cdt,*l_z,bitshift) + multiply(*l_x,l_ydt,bitshift);
  427.  
  428.       *l_x += l_dx;
  429.       *l_y += l_dy;
  430.       *l_z += l_dz;
  431.       return(0);
  432. }
  433.  
  434. int lorenz3d1floatorbit(double *x, double *y, double *z)
  435. {
  436.       double norm;
  437.  
  438.       xdt = (*x)*dt;
  439.       ydt = (*y)*dt;
  440.       zdt = (*z)*dt;
  441.  
  442.       /* 1-lobe Lorenz */
  443.       norm = sqrt((*x)*(*x)+(*y)*(*y));
  444.       dx   = (-adt-dt)*(*x) + (adt-bdt)*(*y) + (dt-adt)*norm + ydt*(*z);
  445.       dy   = (bdt-adt)*(*x) - (adt+dt)*(*y) + (bdt+adt)*norm - xdt*(*z) -
  446.          norm*zdt;
  447.       dz   = (ydt/2) - cdt*(*z);
  448.  
  449.       *x += dx;
  450.       *y += dy;
  451.       *z += dz;
  452.       return(0);
  453. }
  454.  
  455. int lorenz3dfloatorbit(double *x, double *y, double *z)
  456. {
  457.       xdt = (*x)*dt;
  458.       ydt = (*y)*dt;
  459.       zdt = (*z)*dt;
  460.  
  461.       /* 2-lobe Lorenz (the original) */
  462.       dx  = -adt*(*x) + adt*(*y);
  463.       dy  =  bdt*(*x) - ydt - (*z)*xdt;
  464.       dz  = -cdt*(*z) + (*x)*ydt;
  465.  
  466.       *x += dx;
  467.       *y += dy;
  468.       *z += dz;
  469.       return(0);
  470. }
  471.  
  472. int lorenz3d3floatorbit(double *x, double *y, double *z)
  473. {
  474.       double norm;
  475.  
  476.       xdt = (*x)*dt;
  477.       ydt = (*y)*dt;
  478.       zdt = (*z)*dt;
  479.  
  480.       /* 3-lobe Lorenz */
  481.       norm = sqrt((*x)*(*x)+(*y)*(*y));
  482.       dx   = (-(adt+dt)*(*x) + (adt-bdt+zdt)*(*y)) / 3 +
  483.          ((dt-adt)*((*x)*(*x)-(*y)*(*y)) +
  484.          2*(bdt+adt-zdt)*(*x)*(*y))/(3*norm);
  485.       dy   = ((bdt-adt-zdt)*(*x) - (adt+dt)*(*y)) / 3 +
  486.          (2*(adt-dt)*(*x)*(*y) +
  487.          (bdt+adt-zdt)*((*x)*(*x)-(*y)*(*y)))/(3*norm);
  488.       dz   = (3*xdt*(*x)*(*y)-ydt*(*y)*(*y))/2 - cdt*(*z);
  489.  
  490.       *x += dx;
  491.       *y += dy;
  492.       *z += dz;
  493.       return(0);
  494. }
  495.  
  496. int lorenz3d4floatorbit(double *x, double *y, double *z)
  497. {
  498.       xdt = (*x)*dt;
  499.       ydt = (*y)*dt;
  500.       zdt = (*z)*dt;
  501.  
  502.       /* 4-lobe Lorenz */
  503.       dx   = (-adt*(*x)*(*x)*(*x) + (2*adt+bdt-zdt)*(*x)*(*x)*(*y) +
  504.          (adt-2*dt)*(*x)*(*y)*(*y) + (zdt-bdt)*(*y)*(*y)*(*y)) /
  505.          (2 * ((*x)*(*x)+(*y)*(*y)));
  506.       dy   = ((bdt-zdt)*(*x)*(*x)*(*x) + (adt-2*dt)*(*x)*(*x)*(*y) +
  507.          (-2*adt-bdt+zdt)*(*x)*(*y)*(*y) - adt*(*y)*(*y)*(*y)) /
  508.          (2 * ((*x)*(*x)+(*y)*(*y)));
  509.       dz   = (2*xdt*(*x)*(*x)*(*y) - 2*xdt*(*y)*(*y)*(*y) - cdt*(*z));
  510.  
  511.       *x += dx;
  512.       *y += dy;
  513.       *z += dz;
  514.       return(0);
  515. }
  516.  
  517. int henonfloatorbit(double *x, double *y, double *z)
  518. {
  519.       double newx,newy;
  520.       newx  = 1 + *y - a*(*x)*(*x);
  521.       newy  = b*(*x);
  522.       *x = newx;
  523.       *y = newy;
  524.       return(0);
  525. }
  526.  
  527. int henonlongorbit(long *l_x, long *l_y, long *l_z)
  528. {
  529.       long newx,newy;
  530.       newx = multiply(*l_x,*l_x,bitshift);
  531.       newx = multiply(newx,l_a,bitshift);
  532.       newx  = fudge + *l_y - newx;
  533.       newy  = multiply(l_b,*l_x,bitshift);
  534.       *l_x = newx;
  535.       *l_y = newy;
  536.       return(0);
  537. }
  538.  
  539. int rosslerfloatorbit(double *x, double *y, double *z)
  540. {
  541.       xdt = (*x)*dt;
  542.       ydt = (*y)*dt;
  543.  
  544.       dx = -ydt - (*z)*dt;
  545.       dy = xdt + (*y)*adt;
  546.       dz = bdt + (*z)*xdt - (*z)*cdt;
  547.  
  548.       *x += dx;
  549.       *y += dy;
  550.       *z += dz;
  551.       return(0);
  552. }
  553.  
  554. int pickoverfloatorbit(double *x, double *y, double *z)
  555. {
  556.       double newx,newy,newz;
  557.       newx = sin(a*(*y)) - (*z)*cos(b*(*x));
  558.       newy = (*z)*sin(c*(*x)) - cos(d*(*y));
  559.       newz = sin(*x);
  560.       *x = newx;
  561.       *y = newy;
  562.       *z = newz;
  563.       return(0);
  564. }
  565. /* page 149 "Science of Fractal Images" */
  566. int gingerbreadfloatorbit(double *x, double *y, double *z)
  567. {
  568.       double newx;
  569.       newx = 1 - (*y) + fabs(*x);
  570.       *y = *x;
  571.       *x = newx;
  572.       return(0);
  573. }
  574.  
  575. int rosslerlongorbit(long *l_x, long *l_y, long *l_z)
  576. {
  577.       l_xdt = multiply(*l_x,l_dt,bitshift);
  578.       l_ydt = multiply(*l_y,l_dt,bitshift);
  579.  
  580.       l_dx  = -l_ydt - multiply(*l_z,l_dt,bitshift);
  581.       l_dy  =  l_xdt + multiply(*l_y,l_adt,bitshift);
  582.       l_dz  =  l_bdt + multiply(*l_z,l_xdt,bitshift)
  583.              - multiply(*l_z,l_cdt,bitshift);
  584.  
  585.       *l_x += l_dx;
  586.       *l_y += l_dy;
  587.       *l_z += l_dz;
  588.  
  589.       return(0);
  590. }
  591.  
  592. /* OSTEP  = Orbit Step (and inner orbit value) */
  593. /* NTURNS = Outside Orbit */
  594. /* TURN2  = Points per orbit */
  595. /* a      = Angle */
  596.  
  597.  
  598. int kamtorusfloatorbit(double *r, double *s, double *z)
  599. {
  600.    double srr;
  601.    if(t++ >= l_d)
  602.    {
  603.       orbit += b;
  604.       (*r) = (*s) = orbit/3;
  605.       t = 0;
  606.       *z = orbit;
  607.       if(orbit > c)
  608.      return(1);
  609.    }
  610.    srr = (*s)-(*r)*(*r);
  611.    (*s)=(*r)*sinx+srr*cosx;
  612.    (*r)=(*r)*cosx-srr*sinx;
  613.    return(0);
  614. }
  615.  
  616. int kamtoruslongorbit(long *r, long *s, long *z)
  617. {
  618.    long srr;
  619.    if(t++ >= l_d)
  620.    {
  621.       l_orbit += l_b;
  622.       (*r) = (*s) = l_orbit/3;
  623.       t = 0;
  624.       *z = l_orbit;
  625.       if(l_orbit > l_c)
  626.      return(1);
  627.    }
  628.    srr = (*s)-multiply((*r),(*r),bitshift);
  629.    (*s)=multiply((*r),l_sinx,bitshift)+multiply(srr,l_cosx,bitshift);
  630.    (*r)=multiply((*r),l_cosx,bitshift)-multiply(srr,l_sinx,bitshift);
  631.    return(0);
  632. }
  633. /*#define sign(x) ((x)>0?1:((x)<0?(-1):0))*/
  634. #define sign(x) ((x)>=0?1:-1)
  635. int hopalong2dfloatorbit(double *x, double *y, double *z)
  636. {
  637.    double tmp;
  638.    tmp = *y - sign(*x)*sqrt(fabs(b*(*x)-c));
  639.    *y = a - *x;
  640.    *x = tmp;
  641.    return(0);
  642. }
  643.  
  644. int martin2dfloatorbit(double *x, double *y, double *z)
  645. {
  646.    double tmp;
  647.    tmp = *y - sin(*x);
  648.    *y = a - *x;
  649.    *x = tmp;
  650.    return(0);
  651. }
  652.  
  653. /**********************************************************************/
  654. /*   Main fractal engines - put in fractalspecific[fractype].calctype */
  655. /**********************************************************************/
  656.  
  657. int orbit2dfloat()
  658. {
  659.    FILE *fp;
  660.    double *soundvar;
  661.    double x,y,z;
  662.    int color,col,row;
  663.    int count;
  664.    int oldrow, oldcol;
  665.    double *p0,*p1,*p2;
  666.    struct affine cvt;
  667.    int ret;
  668.  
  669.    fp = open_orbitsave();
  670.    /* setup affine screen coord conversion */
  671.    setup_convert_to_screen(&cvt);
  672.  
  673.    /* set up projection scheme */
  674.    if(projection==0)
  675.    {
  676.       p0 = &z;
  677.       p1 = &x;
  678.       p2 = &y;
  679.    }
  680.    else if(projection==1)
  681.    {
  682.       p0 = &x;
  683.       p1 = &z;
  684.       p2 = &y;
  685.    }
  686.    else if(projection==2)
  687.    {
  688.       p0 = &x;
  689.       p1 = &y;
  690.       p2 = &z;
  691.    }
  692.    if(soundflag==1)
  693.       soundvar = &x;
  694.    else if(soundflag==2)
  695.       soundvar = &y;
  696.    else if(soundflag==3)
  697.       soundvar = &z;
  698.  
  699.    count = 0;
  700.    if(inside > 0)
  701.       color = inside;
  702.    if(color >= colors)
  703.       color = 1;
  704.    oldcol = oldrow = -1;
  705.    x = initorbit[0];
  706.    y = initorbit[1];
  707.    z = initorbit[2];
  708.  
  709.    if (resuming)
  710.    {
  711.       start_resume();
  712.       get_resume(sizeof(count),&count,sizeof(color),&color,
  713.          sizeof(oldrow),&oldrow,sizeof(oldcol),&oldcol,
  714.          sizeof(x),&x,sizeof(y),&y,sizeof(z),&z,
  715.          sizeof(t),&t,sizeof(orbit),&orbit,
  716.          0);
  717.       end_resume();
  718.    }
  719.  
  720.    ret = 0;
  721.    while(1)
  722.    {
  723.       if(check_key())
  724.       {
  725.          nosnd();
  726.          alloc_resume(100,1);
  727.          put_resume(sizeof(count),&count,sizeof(color),&color,
  728.             sizeof(oldrow),&oldrow,sizeof(oldcol),&oldcol,
  729.             sizeof(x),&x,sizeof(y),&y,sizeof(z),&z,
  730.             sizeof(t),&t,sizeof(orbit),&orbit,
  731.             0);
  732.          ret = -1;
  733.          break;
  734.       }
  735.  
  736.       if (++count > 1000)
  737.       {        /* time to switch colors? */
  738.      count = 0;
  739.      if (++color >= colors)   /* another color to switch to? */
  740.           color = 1;    /* (don't use the background color) */
  741.       }
  742.  
  743.       col = cvt.a*x + cvt.b*y + cvt.e;
  744.       row = cvt.c*x + cvt.d*y + cvt.f;
  745.       if ( col >= 0 && col < xdots && row >= 0 && row < ydots )
  746.       {
  747.      if (soundflag > 0)
  748.        snd((int)(*soundvar*100+basehertz));
  749.  
  750.      if(oldcol != -1 && connect)
  751.         draw_line(col,row,oldcol,oldrow,color&(colors-1));
  752.      else
  753.         (*plot)(col,row,color&(colors-1));
  754.      oldcol = col;
  755.      oldrow = row;
  756.       }
  757.       else
  758.      oldrow = oldcol = -1;
  759.  
  760.       if(curfractalspecific->orbitcalc(p0, p1, p2))
  761.      break;
  762.       if(fp)
  763.       fprintf(fp,orbitsave_format,*p0,*p1,0.0);
  764.    }
  765.    if(fp)
  766.       fclose(fp);
  767.    return(ret);
  768. }
  769.  
  770. int orbit2dlong()
  771. {
  772.    FILE *fp;
  773.    long *soundvar;
  774.    long x,y,z;
  775.    int color,col,row;
  776.    int count;
  777.    int oldrow, oldcol;
  778.    long *p0,*p1,*p2;
  779.    struct l_affine cvt;
  780.    int ret;
  781.    fp = open_orbitsave();
  782.  
  783.    /* setup affine screen coord conversion */
  784.    l_setup_convert_to_screen(&cvt);
  785.  
  786.    /* set up projection scheme */
  787.    if(projection==0)
  788.    {
  789.       p0 = &z;
  790.       p1 = &x;
  791.       p2 = &y;
  792.    }
  793.    else if(projection==1)
  794.    {
  795.       p0 = &x;
  796.       p1 = &z;
  797.       p2 = &y;
  798.    }
  799.    else if(projection==2)
  800.    {
  801.       p0 = &x;
  802.       p1 = &y;
  803.       p2 = &z;
  804.    }
  805.    if(soundflag==1)
  806.       soundvar = &x;
  807.    else if(soundflag==2)
  808.       soundvar = &y;
  809.    else if(soundflag==3)
  810.       soundvar = &z;
  811.    count = 0;
  812.    if(inside > 0)
  813.       color = inside;
  814.    if(color >= colors)
  815.       color = 1;
  816.    oldcol = oldrow = -1;
  817.    x = initorbitlong[0];
  818.    y = initorbitlong[1];
  819.    z = initorbitlong[2];
  820.  
  821.    if (resuming)
  822.    {
  823.       start_resume();
  824.       get_resume(sizeof(count),&count,sizeof(color),&color,
  825.          sizeof(oldrow),&oldrow,sizeof(oldcol),&oldcol,
  826.          sizeof(x),&x,sizeof(y),&y,sizeof(z),&z,
  827.          sizeof(t),&t,sizeof(l_orbit),&l_orbit,
  828.          0);
  829.       end_resume();
  830.    }
  831.  
  832.    ret = 0;
  833.    while(1)
  834.    {
  835.       if(check_key())
  836.       {
  837.      nosnd();
  838.      alloc_resume(100,1);
  839.      put_resume(sizeof(count),&count,sizeof(color),&color,
  840.             sizeof(oldrow),&oldrow,sizeof(oldcol),&oldcol,
  841.             sizeof(x),&x,sizeof(y),&y,sizeof(z),&z,
  842.             sizeof(t),&t,sizeof(l_orbit),&l_orbit,
  843.             0);
  844.      ret = -1;
  845.      break;
  846.       }
  847.       if (++count > 1000)
  848.       {        /* time to switch colors? */
  849.      count = 0;
  850.      if (++color >= colors)   /* another color to switch to? */
  851.           color = 1;    /* (don't use the background color) */
  852.       }
  853.  
  854.       col = (multiply(cvt.a,x,bitshift) + multiply(cvt.b,y,bitshift) + cvt.e) >> bitshift;
  855.       row = (multiply(cvt.c,x,bitshift) + multiply(cvt.d,y,bitshift) + cvt.f) >> bitshift;
  856.       if ( col >= 0 && col < xdots && row >= 0 && row < ydots )
  857.       {
  858.      if (soundflag > 0)
  859.      {
  860.         double yy;
  861.         yy = *soundvar;
  862.         yy = yy/fudge;
  863.         snd((int)(yy*100+basehertz));
  864.      }
  865.      if(oldcol != -1 && connect)
  866.         draw_line(col,row,oldcol,oldrow,color&(colors-1));
  867.      else
  868.         (*plot)(col,row,color&(colors-1));
  869.      oldcol = col;
  870.      oldrow = row;
  871.       }
  872.       else
  873.      oldrow = oldcol = -1;
  874.  
  875.       /* Calculate the next point */
  876.       if(curfractalspecific->orbitcalc(p0, p1, p2))
  877.      break;
  878.       if(fp)
  879.      fprintf(fp,orbitsave_format,(double)*p0/fudge,(double)*p1/fudge,0.0);
  880.    }
  881.    if(fp)
  882.       fclose(fp);
  883.    return(ret);
  884. }
  885.  
  886. int orbit3dlongcalc()
  887. {
  888.    FILE *fp;
  889.    unsigned count;
  890.    int oldcol,oldrow;
  891.    int oldcol1,oldrow1;
  892.    struct long3dvtinf inf;
  893.    unsigned long maxct;
  894.    int color;
  895.    int ret;
  896.  
  897.    /* setup affine screen coord conversion */
  898.    l_setup_convert_to_screen(&inf.cvt);
  899.  
  900.    oldcol1 = oldrow1 = oldcol = oldrow = -1;
  901.    color = 2;
  902.    if(color >= colors)
  903.       color = 1;
  904.  
  905.    inf.orbit[0] = initorbitlong[0];
  906.    inf.orbit[1] = initorbitlong[1];
  907.    inf.orbit[2] = initorbitlong[2];
  908.  
  909.    if(diskvideo)        /* this would KILL a disk drive! */
  910.    {
  911.       notdiskmsg();
  912.       return(-1);
  913.    }
  914.  
  915.    fp = open_orbitsave();
  916.  
  917.    /* make maxct a function of screen size         */
  918.    maxct = maxit*40L;
  919.    count = inf.ct = 0L;
  920.    ret = 0;
  921.    while(inf.ct++ < maxct) /* loop until keypress or maxit */
  922.    {
  923.       /* calc goes here */
  924.       if (++count > 1000)
  925.       {        /* time to switch colors? */
  926.      count = 0;
  927.      if (++color >= colors)   /* another color to switch to? */
  928.         color = 1;          /* (don't use the background color) */
  929.       }
  930.       if(check_key())
  931.       {
  932.      nosnd();
  933.      ret = -1;
  934.      break;
  935.       }
  936.  
  937.       curfractalspecific->orbitcalc(&inf.orbit[0],&inf.orbit[1],&inf.orbit[2]);
  938.       if(fp)
  939.      fprintf(fp,orbitsave_format,(double)inf.orbit[0]/fudge,(double)inf.orbit[1]/fudge,(double)inf.orbit[2]/fudge);
  940.       if (long3dviewtransf(&inf))
  941.       {
  942.      /* plot if inside window */
  943.      if (inf.col >= 0)
  944.      {
  945.         if(realtime)
  946.            whichimage=1;
  947.         if (soundflag > 0)
  948.         {
  949.            double yy;
  950.            yy = inf.viewvect[soundflag-1];
  951.            yy = yy/fudge;
  952.            snd((int)(yy*100+basehertz));
  953.         }
  954.         if(oldcol != -1 && connect)
  955.            draw_line(inf.col,inf.row,oldcol,oldrow,color&(colors-1));
  956.         else
  957.            (*plot)(inf.col,inf.row,color&(colors-1));
  958.      }
  959.      oldcol = inf.col;
  960.      oldrow = inf.row;
  961.      if(realtime)
  962.      {
  963.         whichimage=2;
  964.         /* plot if inside window */
  965.         if (inf.col1 >= 0)
  966.         {
  967.            if(oldcol1 != -1 && connect)
  968.           draw_line(inf.col1,inf.row1,oldcol1,oldrow1,color&(colors-1));
  969.            else
  970.           (*plot)(inf.col1,inf.row1,color&(colors-1));
  971.         }
  972.         oldcol1 = inf.col1;
  973.         oldrow1 = inf.row1;
  974.      }
  975.       }
  976.    }
  977.    if(fp)
  978.       fclose(fp);
  979.    return(ret);
  980. }
  981.  
  982.  
  983. int orbit3dfloatcalc()
  984. {
  985.    FILE *fp;
  986.    unsigned count;
  987.    int oldcol,oldrow;
  988.    int oldcol1,oldrow1;
  989.    extern int init3d[];
  990.    unsigned long maxct;
  991.    int color;
  992.    int ret;
  993.    struct float3dvtinf inf;
  994.  
  995.    /* setup affine screen coord conversion */
  996.    setup_convert_to_screen(&inf.cvt);
  997.  
  998.    oldcol = oldrow = -1;
  999.    oldcol1 = oldrow1 = -1;
  1000.    color = 2;
  1001.    if(color >= colors)
  1002.       color = 1;
  1003.    inf.orbit[0] = initorbit[0];
  1004.    inf.orbit[1] = initorbit[1];
  1005.    inf.orbit[2] = initorbit[2];
  1006.  
  1007.    if(diskvideo)        /* this would KILL a disk drive! */
  1008.    {
  1009.       notdiskmsg();
  1010.       return(-1);
  1011.    }
  1012.  
  1013.    fp = open_orbitsave();
  1014.  
  1015.    maxct = maxit*40L;
  1016.    count = inf.ct = 0L;
  1017.    ret = 0;
  1018.    while(inf.ct++ < maxct) /* loop until keypress or maxit */
  1019.    {
  1020.       /* calc goes here */
  1021.       if (++count > 1000)
  1022.       {        /* time to switch colors? */
  1023.      count = 0;
  1024.      if (++color >= colors)   /* another color to switch to? */
  1025.         color = 1;          /* (don't use the background color) */
  1026.       }
  1027.  
  1028.       if(check_key())
  1029.       {
  1030.      nosnd();
  1031.      ret = -1;
  1032.      break;
  1033.       }
  1034.  
  1035.       curfractalspecific->orbitcalc(&inf.orbit[0],&inf.orbit[1],&inf.orbit[2]);
  1036.       if(fp)
  1037.       fprintf(fp,orbitsave_format,inf.orbit[0],inf.orbit[1],inf.orbit[2]);
  1038.       if (float3dviewtransf(&inf))
  1039.       {
  1040.      /* plot if inside window */
  1041.      if (inf.col >= 0)
  1042.      {
  1043.         if(realtime)
  1044.            whichimage=1;
  1045.         if (soundflag > 0)
  1046.            snd((int)(inf.viewvect[soundflag-1]*100+basehertz));
  1047.         if(oldcol != -1 && connect)
  1048.            draw_line(inf.col,inf.row,oldcol,oldrow,color&(colors-1));
  1049.         else
  1050.            (*plot)(inf.col,inf.row,color&(colors-1));
  1051.      }
  1052.      oldcol = inf.col;
  1053.      oldrow = inf.row;
  1054.      if(realtime)
  1055.      {
  1056.         whichimage=2;
  1057.         /* plot if inside window */
  1058.         if (inf.col1 >= 0)
  1059.         {
  1060.            if(oldcol1 != -1 && connect)
  1061.           draw_line(inf.col1,inf.row1,oldcol1,oldrow1,color&(colors-1));
  1062.            else
  1063.           (*plot)(inf.col1,inf.row1,color&(colors-1));
  1064.         }
  1065.         oldcol1 = inf.col1;
  1066.         oldrow1 = inf.row1;
  1067.      }
  1068.       }
  1069.    }
  1070.    if(fp)
  1071.       fclose(fp);
  1072.    return(ret);
  1073. }
  1074.  
  1075. /* this function's only purpose is to manage funnyglasses related */
  1076. /* stuff so the code is not duplicated for ifs3d() and lorenz3d() */
  1077. int funny_glasses_call(int (*calc)())
  1078. {
  1079.    int status;
  1080.    status = 0;
  1081.    if(glassestype)
  1082.       whichimage = 1;
  1083.    else
  1084.       whichimage = 0;
  1085.    plot_setup();
  1086.    plot = standardplot;
  1087.    status = calc();
  1088.    if(realtime && glassestype != 3)
  1089.    {
  1090.       realtime = 0;
  1091.       return(status);
  1092.    }
  1093.    if(glassestype && status == 0 && display3d)
  1094.    {
  1095.       if(glassestype==3) /* photographer's mode */
  1096.      if(active_system == 0) { /* dos version */
  1097.         int i;
  1098. static char far firstready[]={"\
  1099. First image (left eye) is ready.  Hit any key to see it,\n\
  1100. then hit <s> to save, hit any other key to create second image."};
  1101.         stopmsg(16,firstready);
  1102.         while ((i = getakey()) == 's' || i == 'S') {
  1103.            diskisactive = 1;
  1104.            savetodisk(savename);
  1105.            diskisactive = 0;
  1106.            }
  1107.         /* is there a better way to clear the screen in graphics mode? */
  1108.         setvideomode(videoentry.videomodeax,
  1109.         videoentry.videomodebx,
  1110.         videoentry.videomodecx,
  1111.         videoentry.videomodedx);
  1112.      }
  1113.      else {           /* Windows version */
  1114. static char far firstready2[]={"First (Left Eye) image is complete"};
  1115.         stopmsg(0,firstready2);
  1116.         clear_screen();
  1117.         }
  1118.       whichimage = 2;
  1119.       plot_setup();
  1120.       plot = standardplot;
  1121.       /* is there a better way to clear the graphics screen ? */
  1122.       if(status = calc())
  1123.      return(status);
  1124.       if(glassestype==3) /* photographer's mode */
  1125.      if(active_system == 0) { /* dos version */
  1126. static char far secondready[]={"Second image (right eye) is ready"};
  1127.         stopmsg(16,secondready);
  1128.      }
  1129.    }
  1130.    return(status);
  1131. }
  1132.  
  1133. /* double version - mainly for testing */
  1134. static int ifs3dfloat()
  1135. {
  1136.    FILE *fp;
  1137.    unsigned long maxct;
  1138.    int color;
  1139.  
  1140.    double newx,newy,newz,r,sum;
  1141.  
  1142.    int k;
  1143.    int ret;
  1144.  
  1145.    struct float3dvtinf inf;
  1146.  
  1147.    float far *ffptr;
  1148.  
  1149.    /* setup affine screen coord conversion */
  1150.    setup_convert_to_screen(&inf.cvt);
  1151.    srand(1);
  1152.  
  1153.    if(diskvideo)        /* this would KILL a disk drive! */
  1154.    {
  1155.       notdiskmsg();
  1156.       return(-1);
  1157.    }
  1158.  
  1159.    inf.orbit[0] = 0;
  1160.    inf.orbit[1] = 0;
  1161.    inf.orbit[2] = 0;
  1162.  
  1163.    fp = open_orbitsave();
  1164.  
  1165.    maxct = maxit*40L;
  1166.    inf.ct = 0L;
  1167.    ret = 0;
  1168.    while(inf.ct++ < maxct) /* loop until keypress or maxit */
  1169.    {
  1170.       if( check_key() )  /* keypress bails out */
  1171.       {
  1172.      ret = -1;
  1173.      break;
  1174.       }
  1175.       r = rand();     /* generate fudged random number between 0 and 1 */
  1176.       r /= 32767;
  1177.  
  1178.       /* pick which iterated function to execute, weighted by probability */
  1179.       sum = ifs_defn[12]; /* [0][12] */
  1180.       k = 0;
  1181.       while ( sum < r)
  1182.       {
  1183.      k++;
  1184.      sum += ifs_defn[k*IFS3DPARM+12];
  1185.      if (ifs_defn[(k+1)*IFS3DPARM+12] == 0) break; /* for safety */
  1186.       }
  1187.  
  1188.       /* calculate image of last point under selected iterated function */
  1189.       ffptr = ifs_defn + k*IFS3DPARM; /* point to first parm in row */
  1190.       newx = *ffptr * inf.orbit[0] +
  1191.          *(ffptr+1) * inf.orbit[1] +
  1192.          *(ffptr+2) * inf.orbit[2] + *(ffptr+9);
  1193.       newy = *(ffptr+3) * inf.orbit[0] +
  1194.          *(ffptr+4) * inf.orbit[1] +
  1195.          *(ffptr+5) * inf.orbit[2] + *(ffptr+10);
  1196.       newz = *(ffptr+6) * inf.orbit[0] +
  1197.          *(ffptr+7) * inf.orbit[1] +
  1198.          *(ffptr+8) * inf.orbit[2] + *(ffptr+11);
  1199.  
  1200.       inf.orbit[0] = newx;
  1201.       inf.orbit[1] = newy;
  1202.       inf.orbit[2] = newz;
  1203.       if(fp)
  1204.       fprintf(fp,orbitsave_format,newx,newy,newz);
  1205.       if (float3dviewtransf(&inf))
  1206.       {
  1207.      /* plot if inside window */
  1208.      if (inf.col >= 0)
  1209.      {
  1210.         if(realtime)
  1211.            whichimage=1;
  1212.         color = getcolor(inf.col,inf.row)+1;
  1213.         if( color < colors ) /* color sticks on last value */
  1214.            (*plot)(inf.col,inf.row,color);
  1215.      }
  1216.      if(realtime)
  1217.      {
  1218.         whichimage=2;
  1219.         /* plot if inside window */
  1220.         if (inf.col1 >= 0)
  1221.         {
  1222.            color = getcolor(inf.col1,inf.row1)+1;
  1223.            if( color < colors ) /* color sticks on last value */
  1224.           (*plot)(inf.col1,inf.row1,color);
  1225.         }
  1226.      }
  1227.       }
  1228.    } /* end while */
  1229.    if(fp)
  1230.       fclose(fp);
  1231.    return(ret);
  1232. }
  1233.  
  1234. int ifs()            /* front-end for ifs2d and ifs3d */
  1235. {
  1236.    if (ifs_defn == NULL && ifsload() < 0)
  1237.       return(-1);
  1238.    if (diskvideo) {         /* this would KILL a disk drive! */
  1239.       notdiskmsg();
  1240.       return(-1);
  1241.       }
  1242.    return((ifs_type == 0) ? ifs2d() : ifs3d());
  1243. }
  1244.  
  1245. int ifs2d()    /* IFS logic shamelessly converted to integer math */
  1246. {
  1247.    FILE *fp;
  1248.    unsigned long maxct,ct;
  1249.    int col;
  1250.    int row;
  1251.    int color;
  1252.    int ret;
  1253.  
  1254.    long localifs[NUMIFS][7];        /* local IFS values */
  1255.    long x,y,newx,newy,r,sum, tempr;
  1256.  
  1257.    int i,j,k;
  1258.    struct l_affine cvt;
  1259.    /* setup affine screen coord conversion */
  1260.    l_setup_convert_to_screen(&cvt);
  1261.  
  1262.    srand(1);
  1263.  
  1264.    for (i = 0; i < NUMIFS; i++)    /* fill in the local IFS array */
  1265.       for (j = 0; j < IFSPARM; j++)
  1266.      localifs[i][j] = ifs_defn[i*IFSPARM+j] * fudge;
  1267.  
  1268.    tempr = fudge / 32767;     /* find the proper rand() fudge */
  1269.  
  1270.    fp = open_orbitsave();
  1271.  
  1272.    /* make maxct a function of screen size         */
  1273.    /* 1k times maxit at EGA resolution seems about right */
  1274.    maxct = (float)maxit*(1024.0*xdots*ydots)/(640.0*350.0);
  1275.    ct = 0L;
  1276.    x = y = 0;
  1277.    ret = 0;
  1278.    while(ct++ < maxct) /* loop until keypress or maxit */
  1279.    {
  1280.       if( check_key() )  /* keypress bails out */
  1281.       {
  1282.      ret = -1;
  1283.      break;
  1284.       }
  1285.       r = rand();     /* generate fudged random number between 0 and 1 */
  1286.       r *= tempr;
  1287.  
  1288.       /* pick which iterated function to execute, weighted by probability */
  1289.       sum = localifs[0][6];
  1290.       k = 0;
  1291.       while ( sum < r)
  1292.       {
  1293.      k++;
  1294.      sum += localifs[k][6];
  1295.      if (localifs[k+1][6] == 0) break;    /* for safety */
  1296.       }
  1297.  
  1298.       /* calculate image of last point under selected iterated function */
  1299.       newx = multiply(localifs[k][0], x, bitshift) +
  1300.       multiply(localifs[k][1], y, bitshift) +
  1301.       localifs[k][4];
  1302.       newy = multiply(localifs[k][2], x, bitshift) +
  1303.       multiply(localifs[k][3], y, bitshift) +
  1304.       localifs[k][5];
  1305.       x = newx;
  1306.       y = newy;
  1307.       if(fp)
  1308.      fprintf(fp,orbitsave_format,(double)newx/fudge,(double)newy/fudge,0.0);
  1309.  
  1310.       /* plot if inside window */
  1311.       col = (multiply(cvt.a,x,bitshift) + multiply(cvt.b,y,bitshift) + cvt.e) >> bitshift;
  1312.       row = (multiply(cvt.c,x,bitshift) + multiply(cvt.d,y,bitshift) + cvt.f) >> bitshift;
  1313.       if ( col >= 0 && col < xdots && row >= 0 && row < ydots )
  1314.       {
  1315.      /* color is count of hits on this pixel */
  1316.      color = getcolor(col,row)+1;
  1317.      if( color < colors ) /* color sticks on last value */
  1318.         (*plot)(col,row,color);
  1319.       }
  1320.    }
  1321.    if(fp)
  1322.       fclose(fp);
  1323.    return(ret);
  1324. }
  1325.  
  1326. static int ifs3dlong()
  1327. {
  1328.    FILE *fp;
  1329.    extern int init3d[];
  1330.    unsigned long maxct;
  1331.    int color;
  1332.    int ret;
  1333.  
  1334.    long localifs[NUMIFS][IFS3DPARM];        /* local IFS values */
  1335.    long newx,newy,newz,r,sum, tempr;
  1336.  
  1337.    int i,j,k;
  1338.  
  1339.    struct long3dvtinf inf;
  1340.    srand(1);
  1341.  
  1342.    /* setup affine screen coord conversion */
  1343.    l_setup_convert_to_screen(&inf.cvt);
  1344.  
  1345.    for (i = 0; i < NUMIFS; i++)    /* fill in the local IFS array */
  1346.       for (j = 0; j < IFS3DPARM; j++)
  1347.      localifs[i][j] = ifs_defn[i*IFS3DPARM+j] * fudge;
  1348.  
  1349.    tempr = fudge / 32767;     /* find the proper rand() fudge */
  1350.  
  1351.    inf.orbit[0] = 0;
  1352.    inf.orbit[1] = 0;
  1353.    inf.orbit[2] = 0;
  1354.  
  1355.    fp = open_orbitsave();
  1356.  
  1357.    maxct = maxit*40L;
  1358.    inf.ct = 0L;
  1359.    ret = 0;
  1360.    while(inf.ct++ < maxct) /* loop until keypress or maxit */
  1361.    {
  1362.       if( check_key() )  /* keypress bails out */
  1363.       {
  1364.      ret = -1;
  1365.      break;
  1366.       }
  1367.       r = rand();     /* generate fudged random number between 0 and 1 */
  1368.       r *= tempr;
  1369.  
  1370.       /* pick which iterated function to execute, weighted by probability */
  1371.       sum = localifs[0][12];
  1372.       k = 0;
  1373.       while ( sum < r)
  1374.       {
  1375.      k++;
  1376.      sum += localifs[k][12];
  1377.      if (localifs[k+1][12] == 0) break;    /* for safety */
  1378.       }
  1379.  
  1380.       /* calculate image of last point under selected iterated function */
  1381.       newx = multiply(localifs[k][0], inf.orbit[0], bitshift) +
  1382.       multiply(localifs[k][1], inf.orbit[1], bitshift) +
  1383.       multiply(localifs[k][2], inf.orbit[2], bitshift) + localifs[k][9];
  1384.       newy = multiply(localifs[k][3], inf.orbit[0], bitshift) +
  1385.       multiply(localifs[k][4], inf.orbit[1], bitshift) +
  1386.       multiply(localifs[k][5], inf.orbit[2], bitshift) + localifs[k][10];
  1387.       newz = multiply(localifs[k][6], inf.orbit[0], bitshift) +
  1388.       multiply(localifs[k][7], inf.orbit[1], bitshift) +
  1389.       multiply(localifs[k][8], inf.orbit[2], bitshift) + localifs[k][11];
  1390.  
  1391.       inf.orbit[0] = newx;
  1392.       inf.orbit[1] = newy;
  1393.       inf.orbit[2] = newz;
  1394.       if(fp)
  1395.      fprintf(fp,orbitsave_format,(double)newx/fudge,(double)newy/fudge,(double)newz/fudge);
  1396.  
  1397.       if (long3dviewtransf(&inf))
  1398.       {
  1399.      /* plot if inside window */
  1400.      if (inf.col >= 0)
  1401.      {
  1402.         if(realtime)
  1403.            whichimage=1;
  1404.         color = getcolor(inf.col,inf.row)+1;
  1405.         if( color < colors ) /* color sticks on last value */
  1406.            (*plot)(inf.col,inf.row,color);
  1407.      }
  1408.      if(realtime)
  1409.      {
  1410.         whichimage=2;
  1411.         /* plot if inside window */
  1412.         if (inf.col1 >= 0)
  1413.         {
  1414.            color = getcolor(inf.col1,inf.row1)+1;
  1415.            if( color < colors ) /* color sticks on last value */
  1416.           (*plot)(inf.col1,inf.row1,color);
  1417.         }
  1418.      }
  1419.       }
  1420.    }
  1421.    if(fp)
  1422.       fclose(fp);
  1423.    return(ret);
  1424. }
  1425.  
  1426. static void setupmatrix(MATRIX doublemat)
  1427. {
  1428.    /* build transformation matrix */
  1429.    identity (doublemat);
  1430.  
  1431.    /* apply rotations - uses the same rotation variables as line3d.c */
  1432.    xrot ((double)XROT / 57.29577,doublemat);
  1433.    yrot ((double)YROT / 57.29577,doublemat);
  1434.    zrot ((double)ZROT / 57.29577,doublemat);
  1435.  
  1436.    /* apply scale */
  1437. /*   scale((double)XSCALE/100.0,(double)YSCALE/100.0,(double)ROUGH/100.0,doublemat);*/
  1438.  
  1439. }
  1440.  
  1441. int orbit3dfloat()
  1442. {
  1443.    display3d = -1;
  1444.    if(0 < glassestype && glassestype < 3)
  1445.       realtime = 1;
  1446.    else
  1447.       realtime = 0;
  1448.    return(funny_glasses_call(orbit3dfloatcalc));
  1449. }
  1450.  
  1451. int orbit3dlong()
  1452. {
  1453.    display3d = -1;
  1454.    if(0 < glassestype && glassestype < 3)
  1455.       realtime = 1;
  1456.    else
  1457.       realtime = 0;
  1458.    return(funny_glasses_call(orbit3dlongcalc));
  1459. }
  1460.  
  1461. int ifs3d()
  1462. {
  1463.    display3d = -1;
  1464.  
  1465.    if(0 < glassestype && glassestype < 3)
  1466.       realtime = 1;
  1467.    else
  1468.       realtime = 0;
  1469.    if(floatflag)
  1470.       return(funny_glasses_call(ifs3dfloat)); /* double version of ifs3d */
  1471.    else
  1472.       return(funny_glasses_call(ifs3dlong));  /* long version of ifs3d     */
  1473. }
  1474.  
  1475.  
  1476.  
  1477. static int long3dviewtransf(struct long3dvtinf *inf)
  1478. {
  1479.    int i,j;
  1480.    double tmpx, tmpy, tmpz;
  1481.    long tmp;
  1482.  
  1483.    if (inf->ct == 1)    /* initialize on first call */
  1484.    {
  1485.       for(i=0;i<3;i++)
  1486.       {
  1487.      inf->minvals[i] =  1L << 30;
  1488.      inf->maxvals[i] = -inf->minvals[i];
  1489.       }
  1490.       setupmatrix(inf->doublemat);
  1491.       if(realtime)
  1492.      setupmatrix(inf->doublemat1);
  1493.       /* copy xform matrix to long for for fixed point math */
  1494.       for (i = 0; i < 4; i++)
  1495.      for (j = 0; j < 4; j++)
  1496.      {
  1497.         inf->longmat[i][j] = inf->doublemat[i][j] * fudge;
  1498.         if(realtime)
  1499.            inf->longmat1[i][j] = inf->doublemat1[i][j] * fudge;
  1500.      }
  1501.    }
  1502.  
  1503.    /* 3D VIEWING TRANSFORM */
  1504.    longvmult(inf->orbit,inf->longmat,inf->viewvect,bitshift);
  1505.    if(realtime)
  1506.       longvmult(inf->orbit,inf->longmat1,inf->viewvect1,bitshift);
  1507.  
  1508.    if(inf->ct <= waste) /* waste this many points to find minz and maxz */
  1509.    {
  1510.       /* find minz and maxz */
  1511.       for(i=0;i<3;i++)
  1512.      if ((tmp = inf->viewvect[i]) < inf->minvals[i])
  1513.         inf->minvals[i] = tmp;
  1514.      else if (tmp > inf->maxvals[i])
  1515.         inf->maxvals[i] = tmp;
  1516.  
  1517.       if(inf->ct == waste) /* time to work it out */
  1518.       {
  1519.      inf->iview[0] = inf->iview[1] = 0L; /* center viewer on origin */
  1520.  
  1521.      /* z value of user's eye - should be more negative than extreme
  1522.             negative part of image */
  1523.      inf->iview[2] = (long)((inf->minvals[2]-inf->maxvals[2])*(double)ZVIEWER/100.0);
  1524.  
  1525.      /* center image on origin */
  1526.      tmpx = (-inf->minvals[0]-inf->maxvals[0])/(2.0*fudge); /* center x */
  1527.      tmpy = (-inf->minvals[1]-inf->maxvals[1])/(2.0*fudge); /* center y */
  1528.  
  1529.      /* apply perspective shift */
  1530.      tmpx += ((double)xshift*(xxmax-xxmin))/(xdots);
  1531.      tmpy += ((double)yshift*(yymax-yymin))/(ydots);
  1532.      tmpz = -((double)inf->maxvals[2]) / fudge;
  1533.      trans(tmpx,tmpy,tmpz,inf->doublemat);
  1534.  
  1535.      if(realtime)
  1536.      {
  1537.         /* center image on origin */
  1538.         tmpx = (-inf->minvals[0]-inf->maxvals[0])/(2.0*fudge); /* center x */
  1539.         tmpy = (-inf->minvals[1]-inf->maxvals[1])/(2.0*fudge); /* center y */
  1540.  
  1541.         tmpx += ((double)xshift1*(xxmax-xxmin))/(xdots);
  1542.         tmpy += ((double)yshift1*(yymax-yymin))/(ydots);
  1543.         tmpz = -((double)inf->maxvals[2]) / fudge;
  1544.         trans(tmpx,tmpy,tmpz,inf->doublemat1);
  1545.      }
  1546.      for(i=0;i<3;i++)
  1547.         view[i] = (double)inf->iview[i] / fudge;
  1548.  
  1549.      /* copy xform matrix to long for for fixed point math */
  1550.      for (i = 0; i < 4; i++)
  1551.         for (j = 0; j < 4; j++)
  1552.         {
  1553.            inf->longmat[i][j] = inf->doublemat[i][j] * fudge;
  1554.            if(realtime)
  1555.           inf->longmat1[i][j] = inf->doublemat1[i][j] * fudge;
  1556.         }
  1557.       }
  1558.       return(0);
  1559.    }
  1560.  
  1561.    /* inf->ct > waste */
  1562.    /* apply perspective if requested */
  1563.    if(ZVIEWER)
  1564.    {
  1565.       if(debugflag==22 || ZVIEWER < 100) /* use float for small persp */
  1566.       {
  1567.      /* use float perspective calc */
  1568.      VECTOR tmpv;
  1569.      for(i=0;i<3;i++)
  1570.         tmpv[i] = (double)inf->viewvect[i] / fudge;
  1571.      perspective(tmpv);
  1572.      for(i=0;i<3;i++)
  1573.         inf->viewvect[i] = tmpv[i]*fudge;
  1574.      if(realtime)
  1575.      {
  1576.         for(i=0;i<3;i++)
  1577.            tmpv[i] = (double)inf->viewvect1[i] / fudge;
  1578.         perspective(tmpv);
  1579.         for(i=0;i<3;i++)
  1580.            inf->viewvect1[i] = tmpv[i]*fudge;
  1581.      }
  1582.       }
  1583.       else
  1584.       {
  1585.      longpersp(inf->viewvect,inf->iview,bitshift);
  1586.      if(realtime)
  1587.         longpersp(inf->viewvect1,inf->iview,bitshift);
  1588.       }
  1589.    }
  1590.  
  1591.    /* work out the screen positions */
  1592.    inf->row = ((multiply(inf->cvt.c,inf->viewvect[0],bitshift) +
  1593.         multiply(inf->cvt.d,inf->viewvect[1],bitshift) + inf->cvt.f)
  1594.         >> bitshift)
  1595.           + yyadjust;
  1596.    inf->col = ((multiply(inf->cvt.a,inf->viewvect[0],bitshift) +
  1597.         multiply(inf->cvt.b,inf->viewvect[1],bitshift) + inf->cvt.e)
  1598.         >> bitshift)
  1599.           + xxadjust;
  1600.    if (inf->col < 0 || inf->col >= xdots || inf->row < 0 || inf->row >= ydots)
  1601.       inf->col = inf->row = -1;
  1602.    if(realtime)
  1603.    {
  1604.       inf->row1 = ((multiply(inf->cvt.c,inf->viewvect1[0],bitshift) +
  1605.             multiply(inf->cvt.d,inf->viewvect1[1],bitshift) +
  1606.             inf->cvt.f) >> bitshift)
  1607.           + yyadjust1;
  1608.       inf->col1 = ((multiply(inf->cvt.a,inf->viewvect1[0],bitshift) +
  1609.             multiply(inf->cvt.b,inf->viewvect1[1],bitshift) +
  1610.             inf->cvt.e) >> bitshift)
  1611.           + xxadjust1;
  1612.       if (inf->col1 < 0 || inf->col1 >= xdots || inf->row1 < 0 || inf->row1 >= ydots)
  1613.      inf->col1 = inf->row1 = -1;
  1614.    }
  1615.    return(1);
  1616. }
  1617.  
  1618. static int float3dviewtransf(struct float3dvtinf *inf)
  1619. {
  1620.    int i;
  1621.    double tmpx, tmpy, tmpz;
  1622.    double tmp;
  1623.  
  1624.    if (inf->ct == 1)    /* initialize on first call */
  1625.    {
  1626.       for(i=0;i<3;i++)
  1627.       {
  1628.      inf->minvals[i] =  100000.0; /* impossible value */
  1629.      inf->maxvals[i] = -100000.0;
  1630.       }
  1631.       setupmatrix(inf->doublemat);
  1632.       if(realtime)
  1633.      setupmatrix(inf->doublemat1);
  1634.    }
  1635.  
  1636.    /* 3D VIEWING TRANSFORM */
  1637.    vmult(inf->orbit,inf->doublemat,inf->viewvect );
  1638.    if(realtime)
  1639.       vmult(inf->orbit,inf->doublemat1,inf->viewvect1);
  1640.  
  1641.    if(inf->ct <= waste) /* waste this many points to find minz and maxz */
  1642.    {
  1643.       /* find minz and maxz */
  1644.       for(i=0;i<3;i++)
  1645.      if ((tmp = inf->viewvect[i]) < inf->minvals[i])
  1646.         inf->minvals[i] = tmp;
  1647.      else if (tmp > inf->maxvals[i])
  1648.         inf->maxvals[i] = tmp;
  1649.       if(inf->ct == waste) /* time to work it out */
  1650.       {
  1651.      view[0] = view[1] = 0; /* center on origin */
  1652.      /* z value of user's eye - should be more negative than extreme
  1653.                negative part of image */
  1654.      view[2] = (inf->minvals[2]-inf->maxvals[2])*(double)ZVIEWER/100.0;
  1655.  
  1656.      /* center image on origin */
  1657.      tmpx = (-inf->minvals[0]-inf->maxvals[0])/(2.0); /* center x */
  1658.      tmpy = (-inf->minvals[1]-inf->maxvals[1])/(2.0); /* center y */
  1659.  
  1660.      /* apply perspective shift */
  1661.      tmpx += ((double)xshift*(xxmax-xxmin))/(xdots);
  1662.      tmpy += ((double)yshift*(yymax-yymin))/(ydots);
  1663.      tmpz = -(inf->maxvals[2]);
  1664.      trans(tmpx,tmpy,tmpz,inf->doublemat);
  1665.  
  1666.      if(realtime)
  1667.      {
  1668.         /* center image on origin */
  1669.         tmpx = (-inf->minvals[0]-inf->maxvals[0])/(2.0); /* center x */
  1670.         tmpy = (-inf->minvals[1]-inf->maxvals[1])/(2.0); /* center y */
  1671.  
  1672.         tmpx += ((double)xshift1*(xxmax-xxmin))/(xdots);
  1673.         tmpy += ((double)yshift1*(yymax-yymin))/(ydots);
  1674.         tmpz = -(inf->maxvals[2]);
  1675.         trans(tmpx,tmpy,tmpz,inf->doublemat1);
  1676.         }
  1677.      }
  1678.       return(0);
  1679.       }
  1680.  
  1681.    /* inf->ct > waste */
  1682.    /* apply perspective if requested */
  1683.    if(ZVIEWER)
  1684.    {
  1685.       perspective(inf->viewvect);
  1686.       if(realtime)
  1687.      perspective(inf->viewvect1);
  1688.    }
  1689.    inf->row = inf->cvt.c*inf->viewvect[0] + inf->cvt.d*inf->viewvect[1]
  1690.         + inf->cvt.f + yyadjust;
  1691.    inf->col = inf->cvt.a*inf->viewvect[0] + inf->cvt.b*inf->viewvect[1]
  1692.         + inf->cvt.e + xxadjust;
  1693.    if (inf->col < 0 || inf->col >= xdots || inf->row < 0 || inf->row >= ydots)
  1694.       inf->col = inf->row = -1;
  1695.    if(realtime)
  1696.    {
  1697.       inf->row1 = inf->cvt.c*inf->viewvect1[0] + inf->cvt.d*inf->viewvect1[1]
  1698.         + inf->cvt.f + yyadjust1;
  1699.       inf->col1 = inf->cvt.a*inf->viewvect1[0] + inf->cvt.b*inf->viewvect1[1]
  1700.         + inf->cvt.e + xxadjust1;
  1701.       if (inf->col1 < 0 || inf->col1 >= xdots || inf->row1 < 0 || inf->row1 >= ydots)
  1702.      inf->col1 = inf->row1 = -1;
  1703.    }
  1704.    return(1);
  1705. }
  1706.  
  1707. static FILE *open_orbitsave()
  1708. {
  1709.    FILE *fp;
  1710.    if (orbitsave && (fp = fopen(orbitsavename,"w")))
  1711.    {
  1712.       fprintf(fp,"pointlist x y z color\n");
  1713.       return fp;
  1714.    }
  1715.    return NULL;
  1716. }
  1717.  
  1718.